function createTree(arr: any) {
    let ids = arr.reduce((prev: any, cur: any) => {
        prev.push(cur.id);
        return prev;
    }, []);

    let newArr = <any[]>[];
    arr.forEach((item: any) => {
        if (!ids.includes(item.pid)) {
            findChild(arr, item);
            newArr.push(item);
        }
    });
    return newArr;
}

function findChild(arr: any, item: any) {
    item.children = [];
    arr.forEach((child: any) => {
        if (child.pid === item.id) {
            item.children.push(child);
            findChild(arr, child);
        }
    });
}

export default createTree;